﻿using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;

namespace UDS.SubModule.UnitiveDocument.Document
{
    /// <summary>
    /// Download 的摘要说明。
    /// </summary>
    public partial class DownloadPage : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            string destFileName = Request.QueryString["destFileName"] != null ? Request.QueryString["destFileName"] : "";
            destFileName = Server.MapPath("../") + destFileName;
            destFileName = Server.UrlDecode(destFileName);

            if (File.Exists(destFileName))
            {
                var fi = new FileInfo(destFileName);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;

                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(destFileName), Encoding.UTF8));
                Response.AppendHeader("Content-Length", fi.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(destFileName);
                Response.Flush();
                Response.End();
            }
            else
            {
                Response.Write("<script langauge=javascript>alert('文件不存在!');history.go(-1);</script>");
                Response.End();
            }

            base.OnLoad(e);
        }
    }
}